package git.jas0nch.final4it.util;

import com.alibaba.fastjson.JSONObject;
import git.jas0nch.final4it.enums.PyEnum;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
 * projectName : final4it
 * className: PyUtils
 * description: TODO 类描述
 *
 * @author : chengjiang@asiainfo.com
 * @date: 2022/05/03
 */
public class PyUtils {

    private final static String python = PyEnum.PY_PYTHON;

    /**
     * @description : 执行python脚本，通过选择的数据和算法模型进行评估
     * @param dataPath ：导入数据集路径
     * @param algorithmName ： 算法模型
     * @return : java.lang.String
     * @author : git.jas0nch
     * date : 2022/5/5
     **/
    public static JSONObject excutePy(String dataPath, String algorithmName){
        try {

            String pyFileRoad = getPyFileRoad("pycodes/excutePy.py");
//            String[] args = new String[] { "python", "E:\\大三\\下\\It_new_tech\\final\\java4py\\final4it\\src\\main\\resources\\pycodes\\excutePy.py" };
            Process proc = Runtime.getRuntime().exec(pyArgs(pyFileRoad, algorithmName, dataPath));  // 执行py文件
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), StandardCharsets.UTF_8));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            JSONObject res = JSONObject.parseObject(sb.toString());
            in.close();
            proc.waitFor();
            return res;
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * @description : TODO
     * @param path
     * @return : java.lang.String[]
     * @author : git.jas0nch
     * date : 2022/5/5
     **/
    public static String[] pyArgs(String path){

        String[] args = new String[2];
        args[0] = python;
        args[1] = path;
        return args;
    }

    public static String[] pyArgs(String path, String... values){
        // d = PyEnum.PY_PYTHON;
        //
        String[] args = new String[values.length+2];
        args[0] = python;
        args[1] = path;
        int cnt = 2;
        for (String value : values) {
            args[cnt++] = value;
        }
        return args;
    }
    // get file:
    @Test
    public static String getPyFileRoad(String name) throws IOException {
//        Enumeration<URL> resources = this.getClass().getClassLoader().getResources("pycodes/LinearRegression/LinearRegression.py");
//        System.out.println(resources.nextElement());
//        System.out.println();
        // e.g. : dataName: ClassificationAndRegressiontree/CART.py
        String str = name;
        Resource resource = new ClassPathResource(str);
        return resource.getFile().getAbsolutePath();
    }


    // 选择模型：



}
